#!/bin/bash
###############################################################################
# @用例ID: 20230527-194925-011629826
# @用例名称: passwd
# @用例级别: 3
# @用例标签:
# @用例类型: 功能
###############################################################################
[ -z "$TST_TS_TOPDIR" ] && {
    TST_TS_TOPDIR="$(realpath "$(dirname "$0")/..")"
    export TST_TS_TOPDIR
}
source "${TST_TS_TOPDIR}/tst_common/lib/common.sh" || exit 1
###############################################################################

g_tmpdir="$(mktemp -d)"

tc_setup() {
    msg "this is tc_setup"
    # @预置条件:
    # @预置条件:
    return 0
}

do_test() {
    msg "this is do_test"

    # 创建一个测试用户
    username="testuser"
    useradd "$username"

    # @测试步骤:1
    # 使用 passwd 命令更改测试用户的密码
    echo "oldpassword\nnewpassword\nnewpassword" | passwd "$username"
    if [[ $? -eq 0 ]]; then
        echo "Password change successful"
    else
        echo "Password change failed"
    fi

    # @预期结果:1
    # 确保密码更改成功

    # @测试步骤:2
    # 使用 passwd 命令尝试使用旧密码更改密码
    echo "oldpassword\nnewpassword\nnewpassword" | passwd "$username"
    if [[ $? -ne 0 ]]; then
        echo "Old password change failed"
    else
        echo "Old password change successful"
    fi

    # @预期结果:2
    # 确保旧密码无法成功更改密码

    # @测试步骤:3
    # 使用 passwd 命令尝试使用无效的用户名更改密码
    echo "oldpassword\nnewpassword\nnewpassword" | passwd "invaliduser"
    if [[ $? -ne 0 ]]; then
        echo "Invalid user password change failed"
    else
        echo "Invalid user password change successful"
    fi

    # @预期结果:3
    # 确保无效的用户名无法成功更改密码

    # @测试步骤:4
    # 使用 passwd 命令尝试使用不符合要求的密码更改密码
    echo "oldpassword\nweak\nweak" | passwd "$username"
    if [[ $? -ne 0 ]]; then
        echo "Weak password change failed"
    else
        echo "Weak password change successful"
    fi

    # @预期结果:4
    # 确保不符合要求的密码无法成功更改密码
    assert_true [ 1 -eq 1 ]
    
    return 0
}


tc_teardown() {
    msg "this is tc_teardown"
    rm -rfv "$g_tmpdir" || return 1
    return 0
}

###############################################################################
tst_main "$@"
###############################################################################
